using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Diagnostics;
using System.Web.Configuration;
using Microsoft.SharePoint;
using System.Xml;
using System.Collections.Specialized;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    public class IndexerJob : SPJobDefinition
    {

        /// <summary>
        /// Initializes a new instance of the ExpirationJob class.
        /// </summary>
        public IndexerJob() : base()
        {
            this.Title = IndexerJobInstaller.INDEXERJOB_FEATURE_ID;
        }

        /// <summary>
        /// Initializes a new instance of the ExpirationJob class.
        /// </summary>
        /// <param name="jobName">Name of the job.</param>
        /// <param name="service">The service.</param>
        /// <param name="server">The server.</param>
        /// <param name="targetType">Type of the target.</param>
        public IndexerJob(string jobName, 
                        SPService service, 
                        SPServer server, 
                        SPJobLockType targetType) : base(jobName, service, server, targetType)
        {
            this.Title = IndexerJobInstaller.INDEXERJOB_FEATURE_ID;
        }

        /// <summary>
        /// Initializes a new instance of the TaskLoggerJob class.
        /// </summary>
        /// <param name="jobName">Name of the job.</param>
        /// <param name="webApplication">The web application.</param>
        public IndexerJob(string jobName, 
                            SPWebApplication webApplication) : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = IndexerJobInstaller.INDEXERJOB_FEATURE_ID;
        }

        /// <summary>
        /// Executes the specified content db id.
        /// </summary>
        /// <param name="contentDbId">The content db id.</param>
        public override void Execute(Guid contentDbId)
        {
            TraceProvider.WriteMessage("Start SharePoint Objects Timer Job.", "IndexerJob.Execute");

            TraceProvider.WriteMessage("Get database.", "IndexerJob.Execute");
            SharePointObjectsDatabase database = SharePointObjectsDatabase.GetDatabase();
            
            VersionInfo version = new VersionInfo(database);
            TraceProvider.WriteMessage("Load site objects to index.", "IndexerJob.Execute");
            IndexSiteObjectList objects = new IndexSiteObjectList(version.License.LicensingMode, WebApplication.Farm.Id, database);
            objects.LoadIndexObjects();
            TraceProvider.WriteMessage(string.Format("Loaded {0} site objects to index.", objects.Items.Count), "IndexerJob.Execute");

            if (objects.Items.Count == 0)
            {
                return;
            }
            TraceProvider.WriteMessage(string.Format("*** Start index webapplication {0}.", WebApplication.Name), "IndexerJob.Execute");
            Indexer indexer = new Indexer(version.License.LicensingMode, database);
            indexer.IndexFarmObjects(objects);
            for (int i = 0; i < WebApplication.Sites.Count; i++)
            {
                indexer.IndexWebApplicationObjects(objects, WebApplication);
                using (SPSite site = WebApplication.Sites[i])
                {
                    TraceProvider.WriteMessage(string.Format(" ** Start index site collection {0} ({1} of {2}).", site.Url, i, WebApplication.Sites.Count), "IndexerJob.Execute");
                    for (int j = 0; j < site.AllWebs.Count; j++)
                    {
                        using (SPWeb web = site.AllWebs[j])
                        {
                            TraceProvider.WriteMessage(string.Format("  * Start index web {0} ({1} of {2}).", web.Url, j, site.AllWebs.Count), "IndexerJob.Execute");
                            indexer.IndexSiteObjects(objects, site, web, WebApplication);
                        }
                    }
                }
            }
        }

    }



}
